package com.lovo.cloud.service.impl;

import com.lovo.cloud.model.dos.MovieOrderDO;
import com.lovo.cloud.dao.MovieOrderDAO;
import com.lovo.cloud.model.dto.MovieOrderDTO;
import com.lovo.cloud.service.MovieOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luo
 * @since 2020-12-03
 */
@Service
@Transactional
public class MovieOrderServiceImpl extends ServiceImpl<MovieOrderDAO, MovieOrderDO> implements MovieOrderService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private MovieOrderDAO movieOrderDAO;

    RabbitTemplate.ConfirmCallback callback=(CorrelationData correlationData, boolean b, String s)->{
        //获得MQ返回的唯一标志orderNum
        String orderNum=  correlationData.getId();
        if(b){
            this.updateOrder(orderNum);
        }

    };

    private boolean updateOrder(String orderNum) {

        boolean bl = false;
        int num = movieOrderDAO.updateTagByOrderNum(orderNum);
        if (num>0) {
            bl = true;
        }
        return bl;
    }


    @Override
    public boolean saveOrder(MovieOrderDO movieOrderDO) {
        boolean bl = false;
        String orderNum = System.currentTimeMillis()+"";
        movieOrderDO.setOrderNum(orderNum);
//        //创建时间
//        LocalDate today = LocalDate.now();
//        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        String dateStr = today.format(fmt);
        movieOrderDO.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        int insert = movieOrderDAO.insert(movieOrderDO);

        if (insert>0) {
            MovieOrderDTO movieOrderDTO = new MovieOrderDTO();
            movieOrderDTO.setOrderNum(orderNum);
            movieOrderDTO.setMovieName(movieOrderDO.getMovieName());
            movieOrderDTO.setMovieNum(movieOrderDO.getMovieNum());
            movieOrderDTO.setMoviePrice(movieOrderDO.getMoviePrice());
            //开启手动确认
            rabbitTemplate.setMandatory(true);//开启消息确认
            //绑定回调函数
            rabbitTemplate.setConfirmCallback(callback);
            //组装关联数据
            CorrelationData correlationData=new CorrelationData(orderNum);

            //发送到MQ
            rabbitTemplate.convertAndSend("directExchange","j177.order",movieOrderDTO,correlationData);
            bl = true;
        }
        return bl;

    }

    @Override
    public void sendTopic(String string) {
        rabbitTemplate.convertAndSend("topicExchange","topic.x",string);
    }

//    @Override
//    public void sendFanout(String string) {
//
//    }


}
